package com.aaa.forum.controller;

import com.aaa.forum.common.AppResult;
import com.aaa.forum.common.ResultCode;
import com.aaa.forum.config.AppConfig;
import com.aaa.forum.model.User;
import com.aaa.forum.model.UserSetting;
import com.aaa.forum.services.ISubscribeService;
import com.aaa.forum.services.IUserService;
import com.aaa.forum.services.IUserSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "关注接口")
@Slf4j
@RestController
@RequestMapping("/subscribe")
public class SubscribeController {

    @Resource
    private ISubscribeService subscribeService;

    @Resource
    private IUserService userService;

    @Resource
    private IUserSettingService userSettingService;

    @ApiOperation("关注")
    @PostMapping("/like")
    public AppResult like(HttpServletRequest request,
                          @ApiParam("被订阅用户id") @RequestParam("subscribedUserId") Long subscribedUserId) {
        // 校验用户是否存在
        User existsUser = userService.selectById(subscribedUserId);
        if (existsUser == null || existsUser.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        // 获取用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        // 调用service
        subscribeService.like(user.getId(), subscribedUserId);
        return AppResult.success();
    }

    @ApiOperation("取消关注")
    @PostMapping("/cancel")
    public AppResult cancel(HttpServletRequest request,
                            @ApiParam("被订阅用户id") @RequestParam("subscribedUserId") Long subscribedUserId) {
        // 校验用户是否存在
        User existsUser = userService.selectById(subscribedUserId);
        if (existsUser == null || existsUser.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        // 获取用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        // 调用service
        subscribeService.cancel(user.getId(), subscribedUserId);
        return AppResult.success();
    }

    @ApiOperation("获取粉丝列表")
    @GetMapping("/getFanList")
    public AppResult<List<User>> getFanList(HttpServletRequest request,
                                            @ApiParam("用户id") @RequestParam(value = "userId", required = false) Long userId) {
        // 获取当前登录用户的信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        // 1. 如果查询自己的列表, 直接返回
        if (userId == null || userId == user.getId()) {
            return AppResult.success(subscribeService.getFanList(user.getId()));
        }

        // 2. 查询别人的粉丝列表, 需要进行校验

        // 校验用户是否存在
        User existsUser = userService.selectById(userId);
        if (existsUser == null || existsUser.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        // 校验是否公开粉丝列表
        UserSetting userSetting = userSettingService.selectByUserId(userId);
        if (userSetting.getFansListState() == 1) {
            log.warn(ResultCode.FAILED_FORBIDDEN.toString());
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        return AppResult.success(subscribeService.getFanList(userId));
    }


    @ApiOperation("获取关注列表")
    @GetMapping("/getSubscribeList")
    public AppResult<List<User>> getSubscribeList(HttpServletRequest request,
                                            @ApiParam("用户id") @RequestParam(value = "userId", required = false) Long userId) {
        // 获取当前登录用户的信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        // 1. 如果查询自己的列表, 直接返回
        if (userId == null || userId == user.getId()) {
            return AppResult.success(subscribeService.getSubscribeList(user.getId()));
        }

        // 2. 查询别人的关注, 需要进行校验

        // 校验用户是否存在
        User existsUser = userService.selectById(userId);
        if (existsUser == null || existsUser.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        // 校验是否公开关注列表
        UserSetting userSetting = userSettingService.selectByUserId(userId);
        if (userSetting.getSubscribeListState() == 1) {
            log.warn(ResultCode.FAILED_FORBIDDEN.toString());
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        return AppResult.success(subscribeService.getSubscribeList(userId));
    }
}
